<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;

//use Illuminate\Support\Facades\Schema;

class CreateV12Migration extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        if (!Schema::hasTable('tag')) {
            Schema::create('tag', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->comment('名称');
                $table->tinyInteger('type')->comment('类型 1-文章标签 2-帖子标签');
                $table->tinyInteger('status')->default(1)->comment('状态 1-正常 2-禁用');
                $table->integer('sort')->default(0)->comment('排序');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->index(['type', 'mini_type']);
                $table->timestamps();
                $table->comment = '标签表';
            });
        }

        if (!Schema::hasColumn('configs', 'mini_type')) {
            Schema::table('configs', function (Blueprint $table) {
                $table->string('mini_type', 50)->index()->comment('小程序类型');
            });
        }

        if (!Schema::hasColumn('articles', 'tag_id')) {
            Schema::table('articles', function (Blueprint $table) {
                $table->integer('tag_id')->unsigned()->index()->default(0)->comment('标签id');
            });
        }

        if (!Schema::hasColumn('articles', 'mini_type')) {
            Schema::table('articles', function (Blueprint $table) {
                $table->string('mini_type', 50)->index()->comment('小程序类型');
            });
        }

        if (!Schema::hasColumn('topics', 'tag_id')) {
            Schema::table('topics', function (Blueprint $table) {
                $table->integer('tag_id')->unsigned()->index()->default(0)->comment('标签id');
            });
        }

        if (!Schema::hasColumn('topics', 'mini_type')) {
            Schema::table('topics', function (Blueprint $table) {
                $table->string('mini_type', 50)->index()->comment('小程序类型');
            });
        }

        if (!Schema::hasTable('theme')) {
            Schema::create('theme', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->unique()->comment('名称');
                $table->integer('use_sum')->comment('累计创建次数');
                $table->string('mini_type', 50)->index()->comment('小程序类型');
                $table->timestamps();
                $table->comment = '帖子话题表';
            });
        }

        if (!Schema::hasTable('topics_theme')) {
            Schema::create('topics_theme', function (Blueprint $table) {
                $table->integer('topic_id')->comment('帖子id');
                $table->integer('theme_id')->comment('话题id');
                $table->unique(['topic_id', 'theme_id']);
                $table->comment = '帖子话题关联表';
            });
        }

        if (!Schema::hasTable('common_words')) {
            Schema::create('common_words', function (Blueprint $table) {
                $table->increments('id');
                $table->string('content')->comment('内容');
                $table->integer('sort')->default(0)->comment('排序值');
                $table->integer('status')->default(1)->comment('状态 1-正常 0-禁用');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->unique(['content', 'mini_type']);
                $table->comment = '评论常用语表';
            });
        }

        if (!Schema::hasTable('mini_program')) {
            Schema::create('mini_program', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name')->comment('名称');
                $table->string('slug', 50)->unique()->comment('标识');
                $table->tinyInteger('status')->comment('状态 1-正常 0-禁用');
                $table->timestamps();
                $table->comment = '小程序表';
            });
        }

        if (!Schema::hasTable('settings')) {
            Schema::create('settings', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name', 50)->default('')->index('idx_name')->comment('配置名');
                $table->string('group', 50)->default('')->comment('分组名');
                $table->text('value')->nullable()->comment('配置');
                $table->string('remark', 100)->default('')->comment('备注信息');
                $table->integer('sort')->default(0)->comment('排序');
                $table->string('mini_type', 50)->nullable()->comment('小程序类型');
                $table->timestamps();
                $table->unique(['group', 'name', 'mini_type']);
                $table->comment = '配置表';
            });
        }

        if (!Schema::hasColumn('members', 'integral')) {
            Schema::table('members', function (Blueprint $table) {
                $table->integer('integral')->default(0)->comment('积分');
            });
        }

        if (!Schema::hasColumn('members', 'phone')) {
            Schema::table('members', function (Blueprint $table) {
                $table->string('phone', 35)->nullable()->index()->comment('手机号');
            });
        }

        if (!Schema::hasColumn('members', 'mini_type')) {
            Schema::table('mini_type', function (Blueprint $table) {
                $table->string('mini_type', 50)->nullable()->comment('小程序类型');
            });
        }

        if (!Schema::hasColumn('members', 'invite_id')) {
            Schema::table('members', function (Blueprint $table) {
                $table->integer('invite_id')->unsigned()->default(0)->comment('邀请人id');
            });
        }

        if (!Schema::hasTable('member_integral_log')) {
            Schema::create('member_integral_log', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('member_id')->unsigned()->index()->comment('用户id');
                $table->string('type', 50)->index()->comment('变更类型');
                $table->integer('change')->comment('变更数目');
                $table->string('content')->comment('内容');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->timestamps();
                $table->index(['member_id', 'mini_type']);
                $table->comment = '用户积分日志表';
            });
        }

        if (!Schema::hasTable('member_sign_log')) {
            Schema::create('member_sign_log', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('member_id')->unsigned()->index()->comment('用户id');
                $table->integer('year')->comment('年份');
                $table->integer('month')->comment('月份');
                $table->integer('continue_day')->comment('连续签到天数');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->timestamps();
                $table->index(['member_id', 'mini_type']);
                $table->index(['member_id', 'year', 'month']);
                $table->comment = '用户签到日志表';
            });
        }

        if (!Schema::hasTable('member_task')) {
            Schema::create('member_task', function (Blueprint $table) {
                $table->increments('id');
                $table->string('slug', 50)->comment('标识');
                $table->integer('max_finish')->nullable()->comment('每日上限完成次数');
                $table->integer('integral')->nullable()->comment('奖励积分');
                $table->string('jump_type')->nullable()->comment('跳转类型');
                $table->string('jump_data')->nullable()->comment('跳转参数');
                $table->string('remark')->comment('备注');
                $table->tinyInteger('status')->nullable()->comment('状态 1-正常 0-禁用');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->timestamps();
                $table->unique(['slug', 'mini_type']);
                $table->comment = '用户任务表';
            });
        }

        if (!Schema::hasTable('member_task_log')) {
            Schema::create('member_task_log', function (Blueprint $table) {
                $table->increments('id');
                $table->integer('member_id')->unsigned()->index()->comment('用户id');
                $table->integer('task_id')->unsigned()->index()->comment('任务id');
                $table->integer('log_id')->unsigned()->index()->comment('积分变更记录id');
                $table->string('slug', 50)->index()->comment('任务标识');
                $table->integer('integral')->comment('奖励积分');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->timestamps();
                $table->index(['member_id', 'mini_type']);
                $table->comment = '用户任务完成记录表';
            });
        }

        if (!Schema::hasTable('sms_log')) {
            Schema::create('sms_log', function (Blueprint $table) {
                $table->bigIncrements('id');
                $table->unsignedBigInteger('member_id')->default(0)->comment('用户id');
                $table->string('type')->default('')->comment('短信类型');
                $table->string('phone')->default('')->comment('手机号码');
                $table->string('code')->default('')->comment('短信验证码');
                $table->string('ip')->default('')->comment('IP地址');
                $table->text('extends')->nullable()->comment('扩展信息');
                $table->integer('create_time')->default(0)->comment('发送时间');
                $table->integer('read_time')->default(0)->comment('验证时间');
                $table->unsignedInteger('verify_count')->default(0)->comment('验证过的次数');

                $table->index('member_id', 'idx_member_id');
                $table->index(['phone', 'type'], 'idx_phone');
                $table->comment = '短信记录';
            });
        }

        if ( !Schema::hasTable('sms_tpl')) {
            Schema::create('sms_tpl', function (Blueprint $table) {
                $table->bigIncrements('id')->comment('自增ID');
                $table->string('title')->default('')->comment('标题');
                $table->string('type')->default('')->index('idx_type')->comment('消息模板类型');
                $table->text('content')->nullable()->comment('短信消息内容，格式：驱动名|消息,驱动名|模板');
                $table->text('template')->nullable()->comment('短信模板，格式：驱动名|模板,驱动名|模板   aliyun|xxx,yunpian|xxxx');
                $table->timestamps();
                $table->comment = '短信模板配置表';
            });
        }

        if (!Schema::hasTable('admin_mini_program')) {
            Schema::create('admin_mini_program', function (Blueprint $table) {
                $table->id();
                $table->integer('admin_id')->unsigned()->comment('管理员ID');
                $table->string('mini_type', 50)->comment('小程序类型');
                $table->unique(['admin_id', 'mini_type']);
                $table->comment = '管理员和小程序关系表';
            });
        }
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
    }
}
